package com.ziven.common.widget.value;

import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;

import com.ziven.easygo.util.ViewHelper;

/**
 * @author Ziven
 */
public class Weight extends Value {

    public static Weight weight(float weight) {
        return new Weight(weight);
    }

    private Weight(float weight) {
        value = weight;
    }

    @Override
    protected void takeEffect(@NonNull ViewHelper<? extends View> helper) {
        value(Float.class, weight -> helper.getViewNulls().doNotNull(view -> {
            LinearLayout.LayoutParams layoutParams;
            ViewGroup.LayoutParams params = view.getLayoutParams();
            if(params instanceof LinearLayout.LayoutParams) {
                layoutParams = (LinearLayout.LayoutParams) params;
            } else if(params instanceof ViewGroup.MarginLayoutParams) {
                layoutParams = new LinearLayout.LayoutParams((ViewGroup.MarginLayoutParams) params);
            } else if(params != null) {
                layoutParams = new LinearLayout.LayoutParams(params);
            } else {
                layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            }
            layoutParams.weight = weight;
            view.setLayoutParams(layoutParams);
        }));
    }
}
